package menu;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import checkers.CheckPiece;
import chesskers.ImagePanel;

public class CheckDisplayPanel extends GameDisplayPanel {

	private boolean locked;

	public CheckDisplayPanel() {
		super();

		locked = false;
		if (state.isLoaded == false) {
			state.board[1][0] = new CheckPiece(true, 1, 0);
			state.board[3][0] = new CheckPiece(true, 3, 0);
			state.board[5][0] = new CheckPiece(true, 5, 0);
			state.board[7][0] = new CheckPiece(true, 7, 0);
			state.board[0][1] = new CheckPiece(true, 0, 1);
			state.board[2][1] = new CheckPiece(true, 2, 1);
			state.board[4][1] = new CheckPiece(true, 4, 1);
			state.board[6][1] = new CheckPiece(true, 6, 1);
			state.board[1][2] = new CheckPiece(true, 1, 2);
			state.board[3][2] = new CheckPiece(true, 3, 2);
			state.board[5][2] = new CheckPiece(true, 5, 2);
			state.board[7][2] = new CheckPiece(true, 7, 2);

			state.board[0][5] = new CheckPiece(false, 0, 5);
			state.board[2][5] = new CheckPiece(false, 2, 5);
			state.board[4][5] = new CheckPiece(false, 4, 5);
			state.board[6][5] = new CheckPiece(false, 6, 5);
			state.board[1][6] = new CheckPiece(false, 1, 6);
			state.board[3][6] = new CheckPiece(false, 3, 6);
			state.board[5][6] = new CheckPiece(false, 5, 6);
			state.board[7][6] = new CheckPiece(false, 7, 6);
			state.board[0][7] = new CheckPiece(false, 0, 7);
			state.board[2][7] = new CheckPiece(false, 2, 7);
			state.board[4][7] = new CheckPiece(false, 4, 7);
			state.board[6][7] = new CheckPiece(false, 6, 7);
		}

		RedrawBoard();
	}

	public void ClickSquare(int x, int y) {

		if (locked && x == state.selectedX && y == state.selectedY) {
			locked = false;

			state.selectedX = -1;
			state.selectedY = -1;

			state.blackGoing = !state.blackGoing;

			state.moveTo = new ArrayList<Point>();
			state.attackTo = new ArrayList<Point>();
			RedrawBoard();

		} else if (state.moveTo.contains(new Point(x, y))
				|| state.attackTo.contains(new Point(x, y))) {
			boolean attacked = false;
			if (state.moveTo.contains(new Point(x, y))) {
				state.board = state.board[state.selectedX][state.selectedY]
						.moveTo(x, y, state.board);
			} else {
				attacked = true;
				state.board = state.board[state.selectedX][state.selectedY]
						.attackTo(x, y, state.board);
			}
			state.moveTo = new ArrayList<Point>();
			state.attackTo = state.board[x][y]
					.getAttackPossibilities(state.board);
			if (state.attackTo.size() != 0 && attacked) {
				locked = true;
				state.selectedX = x;
				state.selectedY = y;
			} else {
				locked = false;
				state.selectedX = -1;
				state.selectedY = -1;

				state.attackTo = new ArrayList<Point>();

				state.blackGoing = !state.blackGoing;
			}

			RedrawBoard();
		} else {
			if (state.board[x][y] != null
					&& state.board[x][y].isBlack == state.blackGoing && !locked) {
				state.selectedX = x;
				state.selectedY = y;

				state.moveTo = state.board[x][y]
						.getMovePossibilities(state.board);
				state.attackTo = state.board[x][y]
						.getAttackPossibilities(state.board);
				RedrawBoard();
			}
		}
	}

	@Override
	public void MouseClickedBoard(MouseEvent e) {
		super.MouseClickedBoard(e);
		TestForWin();
	}

	private void TestForWin() {
		boolean blackWins = true;

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (state.board[i][j] != null && !state.board[i][j].isBlack) {
					blackWins = false;
				}
			}
		}

		if (blackWins) {
			Victory(true, false);
		}

		boolean redWins = true;

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (state.board[i][j] != null && state.board[i][j].isBlack) {
					redWins = false;
				}
			}
		}

		if (redWins) {
			Victory(false, false);
		}

	}

	public void GivePoints(boolean winnerBlack, boolean forfeight) {
		if (winnerBlack) {
			state.player2.checkerWin++;
			if (forfeight) {
				state.player1.checkerForfeit++;
			} else {
				state.player1.checkerLost++;
			}
		} else {
			state.player1.checkerWin++;
			if (forfeight) {
				state.player2.checkerForfeit++;
			} else {
				state.player2.checkerLost++;
			}
		}
		state.UpdateProfiles();
	}

	public void InstantiateBackground() {
		gd_back_manPanel = new ImagePanel(new ImageIcon(
				"img/check_background.png").getImage());
	}

	@Override
	public void Tie() {
		JOptionPane.showMessageDialog(this, "Tie!");
		state.player1.chessTie++;
		state.player2.chessTie++;
		EndGame();
	}

}